package edu.ynu.se.xiecheng.achitectureclass.controller;


import edu.ynu.se.xiecheng.achitectureclass.entity.Shop;
import edu.ynu.se.xiecheng.achitectureclass.entity.vo.Result;
import edu.ynu.se.xiecheng.achitectureclass.service.ShopService;
import edu.ynu.se.xiecheng.achitectureclass.util.FileUploadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author laruui
 * @since 2023-12-07
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Resource
    private ShopService shopService;

    @GetMapping("/pageShop/{current}/{size}/{userId}")
    public Result pageShop(@PathVariable("current") int current,
                           @PathVariable("size") int size,
                           @PathVariable("userId") String userId) {
        log.info(String.valueOf(current));
        log.info(String.valueOf(size));
        log.info(userId);
        return shopService.pageShop(current, size, userId);
    }

    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile multipartFile) {
        // replaceAll 用来替换windows中的\\ 为 /
        log.info(multipartFile.toString());
        return FileUploadUtil.upload(multipartFile).replaceAll("\\\\", "/");
    }

    @PostMapping("/addShop")
    public Result addShop(Shop shop) {
        log.info(shop.toString());
        return shopService.addNewShop(shop);
    }
}

